package com.qsbg.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsbg.entity.Activity;
import com.qsbg.entity.User;
import com.qsbg.entity.vo.ResultVo;
import com.qsbg.entity.vo.UsersPageVo;
import com.qsbg.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/users")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 删除人员
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResultVo deleteUser(@PathVariable Integer id){
        try{
            userService.removeById(id);
            return ResultVo.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.fail("删除出错");
        }
    }

    /**
     * 修改人员
     * @param user
     * @return
     */
    @PutMapping
    public ResultVo updateUser(@RequestBody User user){
        try{
            user.setUpdateTime(new Date());
            userService.updateById(user);
            return ResultVo.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.fail("修改出错");
        }
    }

    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultVo getUser(@PathVariable Integer id){
        try{
            User user = userService.getById(id);
            return ResultVo.ok(user);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.fail("获取用户出错");
        }
    }

    /**
     * 新增人员
     * @param user
     * @return
     */
    @PostMapping
    public ResultVo addUser(@RequestBody User user){
        try{
            user.setCreateTime(new Date());
            user.setUserId("UID"+ IdUtil.simpleUUID());
            user.setOpenId(IdUtil.simpleUUID());
            boolean save = userService.save(user);
            return ResultVo.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.fail("添加出错");
        }
    }

    /**
     * 修改启用状态
     * @param id
     * @param enabled
     * @return
     */
    @PutMapping("/{id}/{enabled}")
    public ResultVo updateEnable(@PathVariable Integer id,@PathVariable boolean enabled){
        User user = userService.getById(id);
        user.setEnabled(enabled);
        if(userService.updateById(user)){
            return ResultVo.ok();
        }else {
            return ResultVo.fail("启用状态修改失败!");
        }
    }



    /**
     * 分页查询
     * @param usersPageVo
     * @return
     */
    @PostMapping("/getuserlist")
    public ResultVo getUsers(@RequestBody UsersPageVo usersPageVo){
        try{
            final IPage<User> page1 = new Page<>(usersPageVo.getPageNum(), usersPageVo.getPageSize());
            final QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.like("profile_id", usersPageVo.getProfileId());
            wrapper.like("real_name", usersPageVo.getRealName());
            wrapper.like("marjor", usersPageVo.getMarjor());
            wrapper.like("`group`", usersPageVo.getGroup());
            wrapper.like("nickname", usersPageVo.getNickname());
            IPage<User> userIPage = userService.page(page1,wrapper);
            return ResultVo.ok(userIPage);
        }catch (Exception e){
            System.out.println(e.toString());
            return ResultVo.fail("分页查询出错");
        }

    }
}
